<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use think\Cookie;

class msg extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存消息
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        // return var_dump($request->param());
        // 先写死登录用户的ID
        $uid = 1;

        $ls['qid'] = $request->param()['qid'];
        $ls['time'] = date("Y-m-d H:i:s",time());
        $ls['content'] = $request->param()['content'];
        $ls['senduid'] = $uid;
        $ls['receiveuid'] = $request->param()['rid'];

        $data[0] = $ls;
        $data[1] = $ls;

        $data[0]['v_senduid'] = $uid;
        $data[0]['v_receiveuid'] = $request->param()['rid'];
        $data[1]['v_senduid'] = $request->param()['rid'];
        $data[1]['v_receiveuid'] = $uid;

        // return var_dump($data);

        // 启动事务
        Db::startTrans();
        try{
            $res = Db::name('massage')->insertAll($data);            
            // 提交事务
            Db::commit();
            $info['status'] = 'ok';
            $info['info'] = '私信已发送';
            return $info;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $info['status'] = 2;
            $info['info'] = '发送失败，请重试';
            return $info;
        }

    }


    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
    }


    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
